<template>
  <el-dialog
    :title="editId ? '编辑轮播图' : '新增轮播图'"
    v-model="dialogVisible"
    width="50%"
    :before-close="handleClose"
  >
    <el-form
      :model="bannerForm"
      :rules="rules"
      ref="banner-Form"
      label-width="100px"
    >
      <el-form-item label="图片名称" prop="title">
        <el-input v-model="bannerForm.title" placeholder="请输入图片名称" />
      </el-form-item>
      <el-form-item label="封面图片" prop="img">
        <el-upload
          action="#"
          list-type="picture-card"
          :auto-upload="false"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :limit="1"
          :on-exceed="limitTips"
          :on-change="changeFile"
          :file-list="fileList"
        >
          <el-icon><Plus /></el-icon>
        </el-upload>
        <el-dialog v-model="Visible">
          <img w-full :src="dialogImageUrl" alt="Preview Image" />
        </el-dialog>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio v-model="bannerForm.status" :label="1"> 启用</el-radio>
        <el-radio v-model="bannerForm.status" :label="2"> 禁用</el-radio>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closeDialog()">取消</el-button>
        <el-button type="primary" @click="submitBanner()"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, onMounted, watch } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
// 导入store
import useBannerStore from "../../stores/banners";
// 声明
const banners = useBannerStore();

// 定义控制弹窗显隐的变量
const dialogVisible = ref(false);

//关闭弹窗？
const handleClose = (done) => {
  ElMessageBox.confirm("确定关闭吗?", "关闭弹窗！", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      done();
      //   重置表单
      resetForm();
    })
    .catch(() => {
      // catch error
    });
};

// 关闭弹窗
const closeDialog = () => {
  resetForm();
};

// 定义表单
let bannerForm = reactive({
  title: "",
  img: "",
  status: 1,
});

// 表单规则
const rules = reactive({
  title: {
    required: true,
    message: "请输入图片名称",
    trigger: "blur",
  },
  img: { required: true, message: "请选择封面图片", trigger: "blur" },
  status: { required: true, message: "请选择图片状态", trigger: "blur" },
});

// 定义编辑id
let editId = ref("");

// 清空表单，关闭弹窗
const resetForm = () => {
  // 重置编辑id
  editId = ref("");
  // 初始化表单
  bannerForm = reactive({
    title: "",
    img: "",
    status: 1,
  });
  //   重置文件列表
  fileList = reactive([]);

  // 关闭弹窗
  dialogVisible.value = false;
};

// 图片路径
const dialogImageUrl = ref("");
// 预览图窗口可视
const Visible = ref(false);
// 上传的文件列表
var fileList = reactive([]);
// 手动移除文件
const handleRemove = (file, fileList) => {
  console.log(file, fileList);
};
// 图片预览函数;
const handlePictureCardPreview = (file) => {
  dialogImageUrl.value = file.url;
  Visible.value = true;
};
// 下载图片
const handleDownload = (file) => {
  console.log(file);
};
// 文件超出限制数量的提示
const limitTips = () => {
  ElMessage({
    type: "warning",
    message: "你只能选择一张图片作为封面",
  });
};
const changeFile = (file) => {
  //   console.log("修改了文件", file);
  //   设置表单封面图
  bannerForm.img = file.raw;
};

// 提交菜单
const submitBanner = () => {
  console.log("bannerForm", bannerForm);
  //   console.log("editId", editId);
  if (editId.value) {
    banners.editBannerAction({ ...bannerForm, id: editId.value });
  } else {
    banners.addBannerAction(bannerForm);
  }
  resetForm();
};

// 接收父组件数据
const acceptValue = (value) => {
  //   console.log("value", value);
  editId.value = value.id;
  bannerForm = reactive({ ...value });
  fileList.push({
    url: "http://localhost:3000" + value.img,
  });
};

// 将变量与函数暴露出来,此代码片段放到下面
defineExpose({
  dialogVisible,
  acceptValue,
});
</script>

<style lang="css" scoped></style>
